package org.framework.gofun.mq;


import java.util.List;

import org.framework.gofun.mq.tag.TagTransformer;
import org.framework.gofun.utils.DeployModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSON;
import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageQueue;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import com.google.common.collect.Lists;


/**
 * Producer，发送消息
 * 
 */
public class Producer {
	private  Logger log = LoggerFactory.getLogger(Producer.class);
	private DefaultMQProducer producer;
	private String groupName = "group_gofun";// group_gofun
	private String namesrvAddr = "192.168.0.41:9876";// 
	private String topicName = "topic_gofun";// topic_gofun
	@Value("${deployModel}")
	private String deployModel;
	private List<String> topicList = Lists.newArrayList();
	
	//初始化方法
	public void init(){
		
		producer = new DefaultMQProducer(groupName);
		if(namesrvAddr!=null){
			producer.setNamesrvAddr(namesrvAddr);
		}
		try {
			producer.start();
			
			log.info("Producer="+groupName+" 消息队列初始化完毕...");
		} catch (MQClientException e) {
			e.printStackTrace();
			log.error("Producer="+groupName+"  消息队列初始化失败...");
		}
	}
	
	public SendResult send(String tag, Object obj){
		return send(topicName,tag,obj);
	}
	
	public SendResult send(String topic, String tag, Object obj){
		
		String body = JSON.toJSONString(obj);
		 Message msg = new Message(topic,tag, body.getBytes());
		 SendResult sendResult = null;
         try {
        	 
        	 if(deployModel.equals(DeployModel.LOCAL)){
 	        	sendResult = new SendResult();
 	        	sendResult.setMsgId("123456");
 	        	sendResult.setMessageQueue(new MessageQueue());
 	        	sendResult.getMessageQueue().setQueueId(0);
 	        	return sendResult;
 	        }
        	 
        	 if(!topicList.contains(topic)){
        		 producer.createTopic(topic, topic, 1);
        		 topicList.add(topic);
        	 }
        	 
	        if(deployModel.equals(DeployModel.RELEASE) || deployModel.equals(DeployModel.TEST) ){
				sendResult = producer.send(msg);
				log.info("发送订单完成MQ消息完毕：sendResult="+sendResult.toString());
				
	        } 
	        
	       
			if(tag.equals(TagTransformer.TAG_ORDER_COMPLETE)){
				//TODO
				//Order order = JSON.parseObject(new String(msg.getBody()),Order.class);
//				log.info("发送订单完成MQ消息完毕：orderId="+order.getOrderID()+",userId="+order.getUserID()+",queueId="+sendResult.getMessageQueue().getQueueId()+",msgId="+sendResult.getMsgId());
				log.info("发送订单完成MQ消息完毕："+new String(msg.getBody()));
			}
		} catch (MQClientException e) {
			e.printStackTrace();
		} catch (RemotingException e) {
			e.printStackTrace();
		} catch (MQBrokerException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//         log.info("发送消息,topic="+topic+",tag="+tag+",queueId="+sendResult.getMessageQueue().getQueueId()+",msgId="+sendResult.getMsgId());
		return sendResult;
	}
	
	
	public void shutdown(){
		producer.shutdown();
	}
	
	public static void main(String[] args) {
		Producer producer = new Producer();
		producer.init();
		producer.send(TagTransformer.TAG_TEST, System.currentTimeMillis());
		producer.shutdown();
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getNamesrvAddr() {
		return namesrvAddr;
	}

	public void setNamesrvAddr(String namesrvAddr) {
		this.namesrvAddr = namesrvAddr;
	}

	public String getTopicName() {
		return topicName;
	}

	public void setTopicName(String topicName) {
		this.topicName = topicName;
	}
}
